const mutations = {
	// 展开 && 收起左侧菜单
	switchMenu(state) {
		state.isCollapse = !state.isCollapse;
	},

	// 点击菜单的时候，如果标签列表中没有这个菜单，那么就往标签列表中添加新菜单
	addTag(state, tagObj) {
		let navList = state.navList;
		let obj = {};

		// 找出标签列表中与当前点击菜单相同的路由信息
		navList.forEach(item => {
			item.actived = false;

			if (item.router === tagObj.router) {
				obj = item;
				item.actived = true;
			}
		})

		// 如果obj不为空，表示标签列表中存在与当前点击的菜单相同的路由，那么将当前这个相同项的actived值设为true
		if (JSON.stringify(obj) === "{}") {
			navList.forEach(item => {
				item.actived = false;
			})

			navList.push(tagObj);
		}
	},

	// 删除标签
	removeTag(state, tagObj) {
		state.navList.forEach((item, index) => {
			if (item.router === tagObj.router) {
				state.navList.splice(index, 1);

				if (tagObj.actived) {
					state.navList[index - 1].actived = true;
				}
			}
		})
	},

	// 删除左侧的标签
	removeLeftTag(state, obj) {
		// 如果当前标签为选中状态，那么删除当前标签与首页之间的所有标签，并清除缓存
		// 否则，判断选中的那项是否在要删除的列表中，如果在，将首页设为选中，否则删除当前与首页之间的标签
		let removeItemList = [];
		let removeNameList = [];

		state.navList.forEach((item, index) => {
			if (item.router === obj.router) {
				state.navList.forEach((j, i) => {
					if (i != 0 && i < index) {
						removeItemList.push(j);
						removeNameList.push(j.name);
					}
				})
			}
		})

		if (!obj.actived) {
			let isFlag = false;

			if (removeItemList.length > 0) {
				removeItemList.forEach((item, index) => {
					if (item.actived) {
						isFlag = true;
					}
				})
			}

			if (isFlag) {
				state.navList[0].actived = true;
			}
		}

		state.navList = state.navList.filter(item => removeItemList.indexOf(item) < 0);
		state.cachedViews = state.cachedViews.filter(item => removeNameList.indexOf(item) < 0);
	},

	// 关闭右侧标签
	removeRigthTag(state, obj) {
		let l = state.navList.length;
		let removeItemList = [];
		let removeNameList = [];

		state.navList.forEach((item, index) => {
			if (item.router === obj.router) {
				state.navList.forEach((j, i) => {
					if (i > index && i < l) {
						removeItemList.push(j);
						removeNameList.push(j.name);
					}
				})
			}
		})


		if (!obj.actived) {
			let isFlag = false;

			if (removeItemList.length > 0) {
				removeItemList.forEach((item, index) => {
					if (item.actived) {
						isFlag = true;
					}
				})
			}

			if (isFlag) {
				state.navList[0].actived = true;
			}
		}

		state.navList = state.navList.filter(item => removeItemList.indexOf(item) < 0);
		state.cachedViews = state.cachedViews.filter(item => removeNameList.indexOf(item) < 0);
	},

	// 删除除首页之外的所有标签
	removeAllTag(state) {
		let arr = [{
			name: "Home",
			title: "首页",
			actived: true,
			router: "/Home",
			meta: {
				keepAlive: false
			}
		}];

		state.navList = arr;
	},

	// 重置导航标签
	resetNav(state) {
		state.navList = [{
			name: "Home",
			title: "首页",
			actived: true,
			router: "/Home",
			meta: {
				keepAlive: false
			}
		}]
	},

	// 删除 404 页面产生的导航
	removeNotPage (state, oldUrl) {
		state.navList.forEach((item, index) => {
			if (item.router === oldUrl) {
				state.navList.splice(index, 1);
				state.navList[0].actived = true;
			}
		})
	},

	// 缓存页面
	cache(state, item) {
		if (state.cachedViews.indexOf(item.name) == -1) {
			state.cachedViews.push(item.name);
		}
	},

	// 清除缓存页面
	clearCache(state, item) {
		state.cachedViews.forEach((j, index) => {
			if (item.name === j) {
				state.cachedViews.splice(index, 1);
			}
		})
	},

	// 清除所有页面缓存
	clearAll(state) {
		state.cachedViews = [];
	}
}

export default mutations